
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class ReadingDataFromCSV {
	static final int NASDAQ = 0, DJIA = 1, NIKKEI = 2, S_AND_P = 3, DAX = 4, FTSE =5, CAC=6,SHANG=7,HANG=8;
	private ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(
			7);

	public ReadingDataFromCSV() {
		for (int i = 0; i < 9; i++)
			// Adds 4 ArrayLists, each containing data for one of the four stock
			// indexes, to the data ArrayList
			data.add(new ArrayList<Double>());

	}

	public void writingStockData() throws IOException {

		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
		String stockDataOutput = "src/parsedStock/";
		String csvFile = "";
		String featureList[] = { "NASDAQIndex", "DJIAIndex", "Nikkei225",
				"S&P500Index", "DAXIndex", "FTSEIndex", "CACIndex", "SHANGHAIIndex", "HANGIndex"  };
	
		int closeStockValIndex = 0;

		// *****************
		// *** STOCKS ******
		// *****************

		for (int i = 0; i < featureList.length; i++) {
			csvFile = "src/" + featureList[i] + ".csv";
			stockDataOutput = "src/parsedStock/parsed" + featureList[i];

			FileWriter stockData = new FileWriter(stockDataOutput);
			PrintWriter stockDataWriter = new PrintWriter(stockData);
			if(i==1)
				closeStockValIndex=1;
			else
				closeStockValIndex=4;

			try {
				br = new BufferedReader(new FileReader(csvFile));
				br.readLine();
				while ((line = br.readLine()) != null) {
					
					// use comma as separator
					String[] stockVals = line.split(cvsSplitBy);
					//System.out.println(line);
					/*
					System.out.println("Stock Val on " + stockVals[0]
							+ " , closed at " + stockVals[closeStockValIndex]);
					System.out.println("\""+stockVals[closeStockValIndex]+"\"");
					*/
					data.get(i).add(Double.parseDouble(stockVals[closeStockValIndex]));
					stockDataWriter.print(stockVals[0]);
					stockDataWriter.print(",");
					stockDataWriter.println(stockVals[closeStockValIndex]);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			stockDataWriter.flush();
			stockDataWriter.close();
			stockData.close();

		}
	}

	public ArrayList<ArrayList<Double>> getStockData() {
		ArrayList<ArrayList<Double>> d=new ArrayList<ArrayList<Double>>();
		for(int i=0;i< 9;i++){
			d.add(data.get(i));
		}
		return d;
	}

}
